package com.example.hasee;

import android.support.v4.app.Fragment;

import com.example.hasee.addinfo.AddInfoFragment;
import com.example.hasee.showinfo.ShowInfoFragment;
import com.example.hasee.usersetting.UserSettingFragment;
import com.example.hasee.weather.WeatherFragment;

import java.util.WeakHashMap;

public class FragmentFactory {
    private FragmentFactory(){

    }
    //定义Map来存储创建好的Fragment
    private static WeakHashMap<String,Fragment> mFragments=new WeakHashMap<>();
    /**
     * 创建Fragment的方法
     * @param pFragmentClass  Fragment的类文件
     * @return  创建好的Fragment
     */
    public static Fragment creat(Class pFragmentClass){
        //判断是否已经创建过Fragent
        if(mFragments.get(pFragmentClass.getName())==null){
            //创建特定的Fragment
            if(pFragmentClass.isAssignableFrom(AddInfoFragment.class)){
                //添加到Map中
                mFragments.put(pFragmentClass.getName(),new AddInfoFragment());
            }else if (pFragmentClass.isAssignableFrom(ShowInfoFragment.class)){
                mFragments.put(pFragmentClass.getName(),new ShowInfoFragment());
            }else if(pFragmentClass.isAssignableFrom(UserSettingFragment.class)){
                mFragments.put(pFragmentClass.getName(),new UserSettingFragment());
            }else if(pFragmentClass.isAssignableFrom(WeatherFragment.class)){
                mFragments.put(pFragmentClass.getName(),new WeatherFragment());
            }else {
                throw new RuntimeException("Fragment文件错误");
            }
        }
        return mFragments.get(pFragmentClass.getName());
    }
}
